package eu.silentspycamera.adabters;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.hardware.Camera;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

public class ExternalStorageAdabter extends Activity{

	private boolean mExternalStorageAvailable = false;
	private boolean mExternalStorageWriteable = false;
	
	// debug tag
	private String TAG = "SSC";
	
	private  String imagePath = null;
	
	public ExternalStorageAdabter() {
		updateExternalStorageState();
	}

	public void SaveImageToFile( byte[] data, Camera camera){
		
		updateExternalStorageState();
		
		String filename = "SpyPhoto" + System.currentTimeMillis() + ".jpg";
		FileOutputStream outStream = null;
		
		try {
			 // create a File object for the parent directory
			File path =  new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
			 + "/SilentSpyCamera/");
			 // have the object build the directory structure, if needed.
			path.mkdirs();
			
			// check that we can write to the sd card and the dir is there
			if (mExternalStorageAvailable && mExternalStorageWriteable && path.isDirectory()){
				// create a File object for the output file
				File outputFile = new File(path, filename);
				Log.d(TAG, "image saved on sdcard: " + outputFile.toString());
				imagePath = outputFile.toString();
				// now attach the OutputStream to the file object, instead of a String representation
				 outStream = new FileOutputStream(outputFile); 
				 outStream.write(data);	 
				 outStream.close();
			} else {
				Log.d(TAG, "sd card not avalable: " + mExternalStorageAvailable + ", or writable: " + mExternalStorageWriteable);
			}
				 			
			Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
			
		} catch (SecurityException e){
			e.printStackTrace();
			Log.e(TAG, "SecurityException: " + e.getMessage());
		}catch (FileNotFoundException e) {
			e.printStackTrace();
			Log.e(TAG, "FileNotFoundException: " + e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			Log.e(TAG, "IOException: " + e.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
			Log.e(TAG, "Exception: " + e.getMessage());
		} finally {
			
		}
		Log.d(TAG, "onPictureTaken - jpeg");
	}
	
	public void insertImageIntoMediaStore(Context context){
		 new MediaScannerNotifier(context, imagePath);
		 Log.d(TAG, "imagePath: " + imagePath);
	
	}

	private void updateExternalStorageState() {
	    String state = Environment.getExternalStorageState();
	    if (Environment.MEDIA_MOUNTED.equals(state)) {
	        mExternalStorageAvailable = mExternalStorageWriteable = true;
	    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
	        mExternalStorageAvailable = true;
	        mExternalStorageWriteable = false;
	    } else {
	        mExternalStorageAvailable = mExternalStorageWriteable = false;
	    }    
	}	
	

    private static class MediaScannerNotifier implements MediaScannerConnectionClient {
    	private String state = Environment.getExternalStorageState();
        private MediaScannerConnection connection;
        private String path;

        public MediaScannerNotifier(Context context, String path) {
            this.path = path;
            connection = new MediaScannerConnection(context, this);
            connection.connect();
        }

        public void onMediaScannerConnected() {
        	if (Environment.MEDIA_MOUNTED.equals(state)){
        		connection.scanFile(path, null); 
        	}  
        }

        public void onScanCompleted(String path, Uri uri) {
        connection.disconnect();
        }
    }
	
}
